# -*- coding: utf-8 -*-

# Copyright (c) 2025 Lijiang
# Licensed under the MIT License
# See LICENSE file for details

"""
视频压缩工具主程序入口
"""

import sys
import os
import logging
from ui.main_window import MainWindow
from PyQt6.QtWidgets import QApplication
# 添加项目根目录到Python路径
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))

# 导入配置
from config import (
    get_app_info, get_log_config, 
    LOG_CONFIG, APP_NAME, APP_VERSION, APP_ORGANIZATION
)

# 配置日志
log_config = get_log_config()
logging.basicConfig(
    level=getattr(logging, log_config['level']),
    format=log_config['format'],
    handlers=[
        logging.FileHandler(log_config['file'], encoding=log_config['encoding']),
        logging.StreamHandler(sys.stdout)
    ]
)
logger = logging.getLogger(__name__)


def main():
    """主函数"""
    try:
        logger.info("开始启动视频压缩工具")
        
        ############################### 创建应用程序 ###############################
        logger.info("创建QApplication")
        app = QApplication(sys.argv)
        app_info = get_app_info()
        app.setApplicationName(app_info['name'])
        app.setApplicationVersion(app_info['version'])
        app.setOrganizationName(app_info['organization'])

        ############################### 创建主窗口 ###############################
        logger.info("创建主窗口")
        window = MainWindow()
        logger.info("显示主窗口")
        window.show()
        
        ############################### 运行应用程序 ###############################
        logger.info("启动应用程序事件循环")
        sys.exit(app.exec())
        
    except Exception as e:
        logger.error(f"程序启动失败: {e}", exc_info=True)
        raise


if __name__ == "__main__":
    main()
